package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.etc.entity.Admin;
import com.etc.service.AdminLoginService;
import com.etc.service.impl.AdminLoginServiceImpl;

/**
 * Servlet implementation class AdminInfoServlet
 */
@WebServlet("/AdminInfo")
@MultipartConfig 
public class AdminInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminLoginService adminLoginService = new AdminLoginServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String adminId = request.getParameter("adminId");
		String nickname = request.getParameter("nickname");
		String mobile = request.getParameter("mobile");
		String adminPassword = request.getParameter("adminPassword");
		// 文件组件
		Part part = request.getPart("images");

		Admin admin = new Admin();
		admin.setAdminId(adminId);
		admin.setNickname(nickname);
		admin.setMobile(mobile);
		admin.setAdminPassword(adminPassword);
		// 判断文件是否上传
				if (part.getSize() != 0) {
					
					// 1.对上传的文件进行MIME类型判断
					String allowImgContentType = "image/jpg,image/jpeg,image/png,image/gif";
					// 获取上传文件的内容类型
					String fileContentType = part.getContentType();
								
					if (allowImgContentType.indexOf(fileContentType) == -1) {
							// 响应结果文件类型不匹配
							out.print("文件类型不匹配,支持image/jpg,image/jpeg,image/png,image/gif!");
							return;
					}else if(part.getSize() > 40000) {
							// 响应结果文件大小不匹配
							out.print("图片大小不能超过40K!");
							return;
					}else {
									
							// 获取原文件名
							String realFileName  = part.getSubmittedFileName(); // QQ.jpg
							// 获取原文件名的拓展名
							String fileSuffix =  realFileName.substring(realFileName.lastIndexOf("."));
							// 存储文件名,保证文件名唯一
							// 时间戳+4位随机数  或 时间戳+4位随机数+原文件名
							StringBuilder fileName = new StringBuilder();
							// 时间戳
							LocalDateTime now = LocalDateTime.now();
							DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
							String time = dateTimeFormatter.format(now);
							// 4位随机数
							int random = (int) ((Math.random() * 9 + 1) * 1000); // 1000-9999
						    // 拼接方式1: 没有保留原文件名
						    fileName.append(time).append(random).append(fileSuffix);  // 202106101704125727.jpg
						    // 拼接方式2： 保留原文件名
							// fileName.append(time).append(random).append(realFileName); // 202106101704125727QQ.jpg
									
							// 关联文件存储的目录
							File file = new File("D:\\file\\img\\"); 
									
							// 定义文件路径
							StringBuilder filePath = new StringBuilder();
							filePath.append(file).append(File.separator).append(fileName);
									
							// 将文件写入指定位置
							part.write(filePath.toString());
							admin.setAdminImgPath(fileName.toString());
					}
				}else {
					// 无文件,采用默认图片
					admin.setAdminImgPath("default-goods-img.jpg");
				}
				boolean flag=adminLoginService.updateAdminInfo(admin );
				if (flag) {
					out.print("修改成功");
					HttpSession session = request.getSession();
					session.setAttribute("ADMIN_LOGIN", (Admin) admin);
				}		
	}

}
